<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="行业名称" prop="occupationalName">
        <el-input
          v-model.trim="form.occupationalName"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="行业描述" prop="occupationalDescription">
        <el-input
          v-model.trim="form.occupationalDescription"
          autocomplete="off"
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import {
    occupationalUpdate,
    occupationalAdd,
  } from '@/api/management/management'
  export default {
    name: 'OccupationalEdit',
    data() {
      return {
        form: {},
        rules: {
          occupationalName: [
            { required: true, trigger: 'blur', message: '请输入行业名称' },
          ],
          occupationalDescription: [
            { required: true, trigger: 'blur', message: '请输入行业描述' },
          ],
        },
        title: '',
        dialogFormVisible: false,
        row: {}, //用于判断编辑or添加
      }
    },
    created() {},
    methods: {
      showEdit(row) {
        this.row = row
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
        this.$emit('fetch-data')
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            //判断编辑or添加
            if (this.row) {
              await occupationalUpdate(this.form).then((res) => {
                if (res.code == 200) {
                  this.$baseMessage(res.data, 'success')
                } else {
                  this.$baseMessage('编辑失败', 'error')
                }
              })
            } else {
              await occupationalAdd(this.form).then((res) => {
                if (res.code == 200) {
                  this.$baseMessage(res.data, 'success')
                } else {
                  this.$baseMessage('添加失败', 'error')
                }
              })
            }
            //置空
            this.$refs['form'].resetFields()
            this.dialogFormVisible = false
            this.$emit('fetchData')
            this.form = this.$options.data().form
          } else {
            return false
          }
        })
      },
    },
  }
</script>
